package com.slkj.shilixiaoyuanapp.ui.main;

import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.content.FileProvider;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.ittianyu.bottomnavigationviewex.BottomNavigationViewEx;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.slkj.shilixiaoyuanapp.BuildConfig;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.base.ActivityInfo;
import com.slkj.shilixiaoyuanapp.base.BaseActivity;
import com.slkj.shilixiaoyuanapp.base.LazyFragmentPagerAdapter;
import com.slkj.shilixiaoyuanapp.data.UserRequest;
import com.slkj.shilixiaoyuanapp.entity.VersionEntity;
import com.slkj.shilixiaoyuanapp.entity.code.FileCode;
import com.slkj.shilixiaoyuanapp.entity.code.RoleCode;
import com.slkj.shilixiaoyuanapp.net.HttpHeper;
import com.slkj.shilixiaoyuanapp.net.callback.CommonCallBack;
import com.slkj.shilixiaoyuanapp.net.down.DownloadInfo;
import com.slkj.shilixiaoyuanapp.ui.main.community.CommunityFragment;
import com.slkj.shilixiaoyuanapp.ui.main.message.MessageMainFragment;
import com.slkj.shilixiaoyuanapp.ui.parent.tool.ParentToolFragment;
import com.slkj.shilixiaoyuanapp.util.UpdataFileUtil;
import com.slkj.shilixiaoyuanapp.view.NoScrollViewPager;
import com.slkj.shilixiaoyuanapp.view.dialog.DownFileDialog;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import butterknife.BindView;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * TODO:activity被系统回收时，底部导航栏选中状态被清除
 */
@ActivityInfo(layout = R.layout.activity_main,isShowActionBar = false)
public class MainActivity extends BaseActivity {
    @BindView(R.id.bnve)
    BottomNavigationViewEx bottomNavigationViewEx;
    @BindView(R.id.vp)
    NoScrollViewPager viewPager;
    @BindView(R.id.fab)
    ImageView toolImg;
    private List<Fragment> fragments = new ArrayList<>();
    VersionEntity versionEntity=null;
    int role;
    @Override
    protected void init() {
        role =  UserRequest.getInstance().getUser().getDefaultRole();
        initFragment();
        bottomNavigationViewEx.enableAnimation(false);
        bottomNavigationViewEx.enableShiftingMode(false);
        bottomNavigationViewEx.enableItemShiftingMode(false);
        viewPager.setAdapter(new LazyFragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragments.get(position);
            }
            @Override
            public int getCount() {
                return fragments.size();
            }
        });
        if (role==RoleCode.ROLE_PARENT){
            viewPager.setCurrentItem(2,false);
            toolImg.setSelected(true);
            bottomNavigationViewEx.getMenu().findItem(R.id.menu_tool).setChecked(true);
        }else {
            viewPager.setCurrentItem(0,false);
            toolImg.setSelected(false);
        }
        bottomNavigationViewEx.setOnNavigationItemSelectedListener(item -> {
            int position = 0;
            switch (item.getItemId()) {
                case R.id.menu_show:
                    position = 0;
                    break;
                case R.id.menu_message:
                    position = 1;
                    break;
                case R.id.menu_tool:
                    position = 2;
                    break;
                case R.id.menu_find:
                    position = 3;
                    break;
                case R.id.menu_my:
                    position = 4;
            }
            viewPager.setCurrentItem(position, false);
            toolImg.setSelected(position == 2);
            return true;
        });
        toolImg.setOnClickListener(v -> {
            toolImg.setSelected(!toolImg.isSelected());

            bottomNavigationViewEx.setCurrentItem(2);
            viewPager.setCurrentItem(2, false);
        });
        bottomNavigationViewEx.setItemIconTintList(null);
        checkVerson();
    }
    /**
     * 检查版本更新
     */
    private void checkVerson() {
        HttpHeper.get().userService()
                .checkVersion(1, BuildConfig.VERSION_NAME)
                .compose(bindToLifecycle())
                .compose(getThread())
                .subscribe(new CommonCallBack<VersionEntity>() {
                    @Override
                    public void onCallBackSuccess(VersionEntity data) {
                        if (data.isHaveNewVersion()){
                            versionEntity=data;
                            QMUIDialog hintDialg = new QMUIDialog.CustomDialogBuilder(MainActivity.this)
                                    .setLayout(R.layout.update_dialog)
                                    .create();
                            hintDialg.setCancelable(false);
                            hintDialg.setCanceledOnTouchOutside(false);
                            hintDialg.show();
                            TextView tv_description=hintDialg.getWindow().findViewById(R.id.tv_description);
                            tv_description.setText(data.getContent());
                            hintDialg.getWindow().findViewById(R.id.tv_update).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    UpdateProgressActivity__JumpCenter.builder(MainActivity.this).setUrl(data.getUrl()).create().go();
                                    //hintDialg.dismiss();
                                }
                            });
                        }
                    }
                });
    }



    private void initFragment() {
        if (role==RoleCode.ROLE_PARENT){
            fragments.add(new ParentToolFragment());
            fragments.add(new MessageMainFragment());
            fragments.add(new ShowFragment());
            fragments.add(new CommunityFragment());
            fragments.add(new ParentsMyFragment());
            bottomNavigationViewEx.getMenu().findItem(R.id.menu_show).setIcon(R.drawable.selector_main_item11);
            toolImg.setImageResource(R.drawable.selector_main_item33);
            bottomNavigationViewEx.getMenu().findItem(R.id.menu_show).setTitle("应用");
            bottomNavigationViewEx.getMenu().findItem(R.id.menu_tool).setTitle("展示");


        }else {
            fragments.add(new ShowFragment());
            fragments.add(new MessageMainFragment());
            fragments.add(new TeacherToolFragment());
            fragments.add(new CommunityFragment());
            fragments.add(new TeacherMyFragment());
        }
    }



    @Override
    protected boolean isShouldHideInput(View v, MotionEvent event) {
        return false;
    }
}
